﻿using EasyTest.Shared.TestAttribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace ZLGCANoptimize
{
    public class 恢复默认
    {
        //static ZLGCANHelper zLGCANHelper = null;
        //static List<ZCAN_Receive_Data> zCAN_Receive_Datas = null;

        [ExposedMethodAttribute("")]
        public bool 恢复出厂设置(int 通道, out string 信息, out string data)
        {
#if ELO20240927
            data = "";

            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                /*
                    10 09 2E 4C 00 02 00 00
                    21 00 00 00 00 00 00 00
                 */
                string 首帧 = "10 09 2E 4C 00 02 00 00";//
                string 帧21 = "21 00 00 00 00 00 00 00";//
                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, 帧21, true, SendType.正常发送);
                if (!result1 || !result2)
                {
                    信息 = "发送数据失败,恢复出厂失败";
                    return false;
                }
              
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join(",", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 00"))//03 6E 4C 00 AA AA AA AA
                        {
                            信息 = "恢复出厂成功";
                            return true;
                        }
                    }
                }

                信息 = "恢复出厂失败";
            }
            catch (Exception ex)
            {
                信息 = "恢复出厂失败" + ex.Message;
            }
            return false;
#else
            data = "";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                /*
                    10 09 2E 4C 00 02 00 00
                    21 00 00 00 00 00 00 00
                 */
                string 首帧 = "10 09 2E 4C 00 02 00 00";//
                string 帧21 = "21 00 00 00 00 00 00 00";//
                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, 帧21, true, SendType.正常发送);
                if (!result1 || !result2)
                {
                    信息 = "发送数据失败,恢复出厂失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join(",", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 00"))//03 6E 4C 00 AA AA AA AA
                        {
                            信息 = "恢复出厂成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "恢复出厂失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "恢复出厂失败" + ex.Message;
                return false;
            }
#endif
        }
        [ExposedMethodAttribute("")]
        public bool 快速休眠(int 通道, out string 信息, out string data)
        {
            data = "";
            信息 = "快速休眠成功";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);

                /*
                    10 09 2E 4C 00 02 00 00
                    21 00 00 00 00 00 00 00
                 */
                string 首帧 = "00 00 00 00 00 00 00 03";//
                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x32F, 首帧, true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,快速休眠失败";
                    return false;
                }
                return true;

                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join(",", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 00"))//03 6E 4C 00 AA AA AA AA
                        {
                            信息 = "快速休眠成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "快速休眠失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "恢复出厂失败" + ex.Message;
                return false;
            }

        }


        //=================================== 2024-09-27 ELO指令更新，以下方法为对应更新的内容 ==========================================
        [ExposedMethodAttribute("")]
        public bool 恢复出厂设置4C00(int 通道, out string 信息, out string data)
        {
            data = "";

            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                _ = zLGCANHelper.RecvData(通道, 0x769, 1000, 100);
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                /*
                    10 09 2E 4C 00 02 00 00
                    21 00 00 00 00 00 00 00
           
                 */
                string 首帧 = "04 2E 4C 00 02 00 00 00";//
                bool result1;
                result1 = zLGCANHelper.SentData(通道, 0x761, 首帧, true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,恢复出厂失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 500);

                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join(",", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 00"))//03 6E 4C 00 AA AA AA AA
                        {
                            信息 = "恢复出厂成功";
                            return true;
                        }
                    }
                }

                信息 = "恢复出厂失败";
            }
            catch (Exception ex)
            {
                信息 = "恢复出厂失败" + ex.Message;
            }
            return false;
        }
    }
}
